package com.morningyet.bug.log4j2.application.config;

import com.morningyet.bug.log4j2.application.cache.CaffeineTemplate;
import com.morningyet.bug.log4j2.application.controller.vo.UserVO;
import com.morningyet.bug.log4j2.application.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 默认页,拦截器配置
 *
 * @author moringyet
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    @Qualifier("userCaffeineTemplate")
    private CaffeineTemplate<String, UserVO> caffeineTemplate;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/login.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration
                = registry.addInterceptor(new AuthInterceptor(caffeineTemplate));

        // 所有路径都被拦截
        registration.addPathPatterns("/**");

        // 添加不拦截路径
        registration.excludePathPatterns(
                "/",
                "/login.html",
                "/error",
                "/user/login",
                "/**/*.html",
                "/**/*.js",
                "/**/*.jpg",
                "/**/*.gif",
                "/**/*.ico",
                "/**/*.css",
                "/**/*.woff",
                "/**/*.woff2",
                "/**/*.otf",
                "/**/*.eot",
                "/**/*.svg",
                "/**/*.ttf"
        );
    }

}
